﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using Palluk.PassMaster.Passwords;

namespace Palluk.PassMaster.ViewModel
{
    public class MainWindowViewModel : ViewModelBase
    {
        #region ICommands

        private ICommand _generateCommand;
        public ICommand GenerateCommand
        {
            get
            {
                if (_generateCommand == null)
                    _generateCommand = new DelegateCommand<object>(p => FillPasswordListView());
                return _generateCommand;
            }
        }

        #endregion

        #region properties

        Contract currentContract;

        private ObservableCollection<ListBoxItem> availPasswords = new ObservableCollection<ListBoxItem>();
        public IEnumerable<ListBoxItem> AvailPassword { get { return availPasswords; } }

        private bool IsConsistentData { get; set; }

        public int Minimum
        {
            get { return currentContract.Min; }
            set
            {
                if (currentContract.Min != value)
                {
                    if (value > currentContract.Max || value <= 0)
                        throw new ApplicationException();
                    currentContract.Min = value;
                    OnPropertyChanged("Minimum");
                }
            }
        }

        public int Maximum
        {
            get { return currentContract.Max; }
            set
            {
                if (currentContract.Max != value)
                {
                    if (value < currentContract.Min || value > 20)
                        throw new ApplicationException();
                    currentContract.Max = value;
                    OnPropertyChanged("Maximum");
                    OnPropertyChanged("LimitNumericsChars");
                    OnPropertyChanged("LimitUpcasesChars");
                    OnPropertyChanged("LimitSymbolsChars");
                }
            }
        }

        public int CurrentNumericsChars
        {
            get { return currentContract.numerics; }
            set
            {
                if (currentContract.numerics != value)
                {
                    currentContract.numerics = value;
                    OnPropertyChanged("CurrentNumericsChars");
                }
            }
        }

        public int CurrentUpcasesChars
        {
            get { return currentContract.upcases; }
            set
            {
                if (currentContract.upcases != value)
                {
                    currentContract.upcases = value;
                    OnPropertyChanged("CurrentUpcasesChars");
                }
            }
        }

        public int CurrentSymbolsChars
        {
            get { return currentContract.symbols; }
            set
            {
                if (currentContract.symbols != value)
                {
                    currentContract.symbols = value;
                    OnPropertyChanged("CurrentSymbolsChars");
                }
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Design time constructor
        /// </summary>
        public MainWindowViewModel()
        {
            currentContract = new Contract(8, 12);
            PasswordBuilder builder = new PasswordBuilder();
            availPasswords.Add(new ListBoxItem() { Content = builder.Generate(currentContract) });
            availPasswords.Add(new ListBoxItem() { Content = builder.Generate(currentContract) });
            availPasswords.Add(new ListBoxItem() { Content = builder.Generate(currentContract) });
        }

        /// <summary>
        /// ViewModel constructor for MainWindow window.
        /// </summary>
        /// <param name="mainWindowView">View to bind.</param>
        public MainWindowViewModel(IView mainWindowView)
        {
            mainWindowView.DataContext = this;
            currentContract = new Contract(8, 8);

            mainWindowView.Show();
        }

        #endregion

        #region private and protected methods

        private void ValidateLimits()
        {

        }

        /// <summary>
        /// Create a set of suggested passwords.
        /// </summary>
        private void FillPasswordListView()
        {
            int max = 5;
            PasswordBuilder builder = new PasswordBuilder();

            if (availPasswords.Count > 0)
                availPasswords.Clear();
            for (int c = 0; c < max; c++)
                availPasswords.Add(new ListBoxItem() { Content = builder.Generate(currentContract) } );
        }

        #endregion
    }
}
